﻿using System;
using System.Collections.Generic;
using System.Web;

namespace ReturnDB.Taobao.WebApp
{
    /// <summary>
    ///Client 的摘要说明
    /// </summary>
    public static class Client
    {
        /// <summary>
        /// 获取用户昵称
        /// </summary>
        public static string UserNick
        {
            get
            {
                try
                {
                    if ( HttpContext.Current.Request.Cookies[Config.CookiesName] != null )
                    {
                        return HttpUtility.UrlDecode( HttpContext.Current.Request.Cookies[Config.CookiesName]["visitor_nick"].ToString() );
                    }
                    else
                    {
                        return "";
                    }
                }
                catch ( Exception exp )
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 获取用户授权
        /// </summary>
        public static string Session
        {
            get
            {
                try
                {
                    if ( HttpContext.Current.Request.Cookies[Config.CookiesName] != null )
                    {
                        return HttpContext.Current.Request.Cookies[Config.CookiesName]["top_session"].ToString();
                    }
                    else
                    {
                        return "";
                    }
                }
                catch ( Exception exp )
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 保存用户指定数据  -- 仅debug使用
        /// </summary>
        /// <param name="nick"></param>
        /// <param name="sessionkey"></param>
        public static void SetCookies( string nick , string sessionkey )
        {
            HttpContext.Current.Response.Cookies[Config.CookiesName]["visitor_nick"] = HttpUtility.UrlEncode( nick );
            HttpContext.Current.Response.Cookies[Config.CookiesName]["top_session"] = sessionkey;
        }

        /// <summary>
        /// 用户是否登陆
        /// </summary>
        public static bool IsLogin
        {
            get
            {
                if ( HttpContext.Current.Request.Cookies[Config.CookiesName] != null )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 退出用户清除Cookie
        /// </summary>
        /// <returns></returns>
        public static int Logout()
        {
            HttpContext.Current.Response.Cookies.Remove( Config.CookiesName );
            return 1;
        }
    }
}